package com.pure.share;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareConfig;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.Map;


public class ShareHelper implements UMShareListener, UMAuthListener {

    private static ShareHelper mShare = null;
    private static Activity mActivity = null;
    private static ClickListener mListener;
    private static ProgressDialog loadingDialog;

    public static UMShareAPI getShareAPI(Context context) {
        UMShareAPI mShareAPI;
        UMShareConfig config = new UMShareConfig();
        config.isNeedAuthOnGetUserInfo(true);

        mShareAPI = UMShareAPI.get(context);
        mShareAPI.setShareConfig(config);
        return mShareAPI;
    }

    /**
     * 分享用
     *
     * @param ac
     * @return
     */
    public static ShareHelper getInstance(Activity ac) {
        mActivity = ac;
        if (mShare == null) {
            mShare = new ShareHelper();
        }
        return mShare;
    }

    /**
     * 登录用
     *
     * @param ac
     * @param listener
     * @return
     */
    public static ShareHelper getInstance(Activity ac, ProgressDialog dialog, ClickListener listener) {
        mActivity = ac;
        mListener = listener;
        loadingDialog = dialog;
        if (mShare == null) {
            mShare = new ShareHelper();
        }
        return mShare;
    }


    //-----------------------------------友盟分享回调-----------------------------------
    @Override
    public void onStart(SHARE_MEDIA share_media) {
    }

    @Override
    public void onResult(SHARE_MEDIA share_media) {//分享成功
        Toast.makeText(mActivity, "分享成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        Toast.makeText(mActivity, "分享失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        Toast.makeText(mActivity, "取消分享", Toast.LENGTH_SHORT).show();
    }
//-----------------------------------友盟登录回调-----------------------------------


    @Override
    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
        if (mListener != null) {
            mListener.onAuthComplete(map);
        } else {
            Toast.makeText(mActivity, "授权失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
        Log.i("lxl", i + "," + throwable.toString());
        Toast.makeText(mActivity, "授权失败", Toast.LENGTH_SHORT).show();
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media, int i) {
        Toast.makeText(mActivity, "取消授权", Toast.LENGTH_SHORT).show();
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    /**
     * 分享连接
     *
     * @param title
     * @param imgUrl
     */
    public void share(final Activity context, String title, String imgUrl,String url) {
//        UMImage image = new UMImage(ShareActivity.this, bitmap);//bitmap文件
        UMImage image;
        UMImage thumb;
        if (!TextUtils.isEmpty(imgUrl)) {
            image = new UMImage(context, imgUrl);//封面
            thumb = new UMImage(context, imgUrl);
        } else {
            image = new UMImage(context, R.drawable.ic_logo);//封面
            thumb = new UMImage(context, R.drawable.ic_logo);
        }
        image.setThumb(thumb);
        UMWeb web = new UMWeb(url);
        web.setTitle("借米花");//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription("我在“借米花”上借款，放款速度超快...");//描述
        new ShareAction(context)
                .withMedia(image)
                .withMedia(web)
                .setDisplayList( SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
//                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE)
                .setCallback(this).open();

    }

    /**
     * 分享图片
     */
    public void share(final Activity context, Bitmap bitmap) {
        UMImage image;
        UMImage thumb;
        image = new UMImage(context, bitmap);
        thumb = new UMImage(context, bitmap);
        image.setThumb(thumb);
        new ShareAction(context)
                .withMedia(image)
                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.SINA)
                .setCallback(this).open();

    }
    /**
     * 分享连接
     *
     * @param title
     * @param imgUrl
     */
    public void share(SHARE_MEDIA shareMedia, String title, String description, String imgUrl, String linkUrl) {
        UMImage image;
        UMImage thumb;
        if (!TextUtils.isEmpty(imgUrl)) {
            image = new UMImage(mActivity, imgUrl);//封面
            thumb = new UMImage(mActivity, imgUrl);
        } else {
            image = new UMImage(mActivity, R.drawable.logo);//封面
            thumb = new UMImage(mActivity, R.drawable.logo);
        }
        image.setThumb(thumb);
        UMWeb web = new UMWeb(linkUrl);
        web.setTitle(title);//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription(description);//描述
        new ShareAction(mActivity)
                .setPlatform(shareMedia)
                .withMedia(web)
                .setCallback(this)
                .share();
    }
    public interface ClickListener {
        /**
         * 授权成功
         */
        void onAuthComplete(Map<String, String> map);
    }
}